/*
 * The following is the animated panel which perform and like iPhone UI
 * The coding method to use this animated panel is 
 * <div id="container">
 * 	<ul>
 * 		<li>
 * 			<div>
 * 				content
 * 			</div>
 * 		</li>
 * 	</ul>
 * </div>
 * $('#container).winerdyPanel();
 */
;(function($){
	$.winerdyPanel_current = 0;
	$.winerdyPanel = {
		Move:function(id,n) {
			if($.winerdyPanel_current == n)
				return;
		   if(n == null)
				n = 0;
		   var container = $('#'+id);
		   var item = $(container).children('ul').children('li').eq(n);			   
		   var moveDistance = $(container).closest('.wiNerdy_container').offset().left-$(item).offset().left;
		   $(container).animate({left:'+='+moveDistance},{
			   complete:function(){
			   if($(item).attr('ajax') != null) {
				   $(item).subcontent({
					   url:$(item).attr('ajax'),
					   show:false,
					   afterShow:function(){
					   		$.winerdyPanel.AdjustPanel(id,item);
				       }	
				   });
				   if($(item).attr('refresh') === false)
					   $(item).removeAttr('ajax');
			   }
			   $.winerdyPanel.AdjustPanel(id,item);
		   		}
		   });
		   
		   $.winerdyPanel_current = n;
	   	},
	   MoveById:function(container_id,id){
	 	   if(id == null)
	 		   return;   
		   var item = $('#'+container_id).children('ul').children('li#'+id).index();
		   if(item != -1)
			   $.winerdyPanel.Move(container_id,item);
	   },
	   Next:function(id) {
		   var totalLi = $('#'+id).children('ul').children('li').length;
		   var n = $.winerdyPanel_current+1;
		   if(totalLi <= n)
			   n = 0;
		   $.winerdyPanel.Move(id,n);
	   },
	   Prev: function(id) {
		   var n = $.winerdyPanel_current-1;
		   if(n<0)
			   n = $('#'+id).children('ul').children('li').length-1;
		   $.winerdyPanel.Move(id,n);
	   },
	   AdjustPanel:function(id,item){
		   if(id==null || item == null)
			   return;
		   var container = $('#'+id);			   
		   var heightDistance = $(item).height() - $(container).closest('.wiNerdy_container').height()+5;
		   var lastDom = null;
		   $(container).closest('.wiNerdy_container').animate({height:'+='+heightDistance},{
			   complete:function(){
			   $(item).parents('.wiNerdy_container').map(function (i,dom) {
				   if(i>0){
					   var actualheight = $(lastDom).closest('li').height();
					   $(this).height(actualheight);
				   }
					   lastDom = this;
			   });
		   		}
		   });
	   },
	   AddPanel:function(id,options) {
		   if(id == null)
			   return;
		   var myOptions = {
			   type:'html',
			   content:''
		   };
		   $.extend(myOptions,options);
		   var contaienr = $('#'+id);
		   $(container).children('ul').append('li');
		   var item = $(container).children('ul').children('li:last');
		   if(myOptions.type == 'html'){
			   
		   }
	   }
	};
    $.fn.winerdyPanel = function(options) {
    	if($(this) == null)
    		return false;
        var myoptions = {
            menu:'',
            defaultPanel:0
        };
        $.extend(myoptions, options);
        $.winerdyPanel_current = myoptions.defaultPanel;
        var container = $(this);
        $(container).addClass('wiNerdy_animatedContainer');
        $(container).wrap('<div class="wiNerdy_container" />');
        var wrapper = $(container).closest('.wiNerdy_container');
        $(wrapper).width($(wrapper).parent().width()-1);
        var item = $(container).children('ul').children('li');
		var i = $(container).offset().left;
		$(item).each(function(){
			$(this).width($(wrapper).width());
		});
    };
})(jQuery);